var express = require('express');
var multer = require('connect-multiparty')
var XLSX = require('xlsx');
var router = express.Router();
const multiparMiddleware = multer()

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

// 批量导入 如果存在相同用户则忽略
router.post('/uploadFile', multiparMiddleware, async function (req, res, next) {
  let excelData = [];   //用来保存
  let reqData = [];
  const workbook = XLSX.readFile(req.files.file.path);
  const sheetNames = workbook.SheetNames;
  for (var sheet in workbook.Sheets) {
    if (workbook.Sheets.hasOwnProperty(sheet)) {
      //fromTo = workbook.Sheets[sheet]['!ref'];
      //解析excel文件得到数据
      excelData = excelData.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
    }
  }
  // console.log(excelData);
  for (var i = 0; i < excelData.length; i++) {
    reqData.push({
      id: excelData[i]['序号'],
      className: excelData[i]['班级'],
      numbers: excelData[i]['人数'],
      date: excelData[i]['开班时间'],
      dateStart: excelData[i]['计划就业起点'],
      dateEnd: excelData[i]['计划就业完成'],
      note: excelData[i]['备注'],
    })
  }
  //数据处理结束调用sql语句，并且返回前台
  // console.log(reqData);
  res.json(reqData)
})

module.exports = router;
